﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Milo.Framework
{
    public class Control : IControl
    {
        private const char idSeparator = '.';
        private IControl parent;

        #region constructors
        public Control() 
        {
            Visible = true;
			Enabled = true;
        }

        public Control(IControl parent)
            : this()
        {
            this.parent = parent;
        }

        public Control(string name, IControl parent)
            : this(parent)
        {
            this.Name = name;
        }
        #endregion

        private string modelID;
        private string clientID;
        private string sigatureID;

        #region IControl Members

        public bool Enabled { get; set; }

        public bool Visible { get; set; }

        public string Name { get; set; }

        public IControl Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        public string ModelID
        {
            get
            {
                if (modelID == null)
                    if (parent == null)
                        modelID = Name;
                    else
                        modelID = parent.GetModelIDPrefix() + Name;

                return modelID;
            }
        }

        public string ClientID
        {
            get
            {
                if (clientID == null)
                    clientID = ModelID.Replace('.', '_').Replace('[', '_').Replace("]", "");

                return clientID;
            }
        }

        public string SignatureID 
        {
            get 
            {
                if (parent == null)
                    return Name;
                else
                    return parent.GetModelType().FullName + idSeparator + Name;
            }
        }

        public virtual string GetModelIDPrefix()
        {
            if (string.IsNullOrEmpty(Name))
                return string.Empty;
            else
                return ModelID + idSeparator;
        }

        public virtual Type GetModelType()
        {
            return null;
        }
        #endregion
    }
}
